package com.authine.cloudpivot.controller;

import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.domain.Dto.SXQDDto;
import com.authine.cloudpivot.engine.api.facade.BizObjectFacade;
import com.authine.cloudpivot.engine.api.facade.OrganizationFacade;
import com.authine.cloudpivot.engine.api.model.organization.UserModel;
import com.authine.cloudpivot.engine.api.model.runtime.BizObjectModel;
import com.authine.cloudpivot.engine.enums.status.WorkflowInstanceStatus;
import com.authine.cloudpivot.foundation.orm.api.dml.JPANativeQuery;
import com.authine.cloudpivot.util.Result;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/JGWG")
@Slf4j
public class JGWGController extends BaseController {

    private static final String SCHMEA_CODE = "sxqdb";

    private static final String ORIGINATOR_USERNAME = "ljy";

    @Autowired
    private OrganizationFacade organizationFacade;

    @Autowired
    BizObjectFacade bizObjectFacade;

    @Autowired
    private JPANativeQuery jpaNativeQuery;

    @GetMapping("/getInfo")
    public Result getInfo(@RequestParam("id") String id){
        String sql =String.format("SELECT\n" +
                        "\tt1.id,\n" +
                        "\tt2.NAME jd,\n" +
                        "\tt3.NAME sq,\n" +
                        "\tt4.NAME wg,\n" +
                        "\tt1.jqbh,\n" +
                        "\tt1.bjlx,\n" +
                        "\tt1.jjlb,\n" +
                        "\tt1.jjxz,\n" +
                        "\tt1.bjsj,\n" +
                        "\tt1.afdd,\n" +
                        "\tt1.bjnr,\n" +
                        "\tt1.sszhzxmc,\n" +
                        "\tt1.gxdw,\n" +
                        "\tt1.bjrxm,\n" +
                        "\tt1.bjdh,\n" +
                        "\tt1.dhdz,\n" +
                        "\tt1.jqmc \n" +
                        "FROM\n" +
                        "\t`iknn4_sxqdb` t1\n" +
                        "\tLEFT JOIN h_org_department t2 ON t1.belongJd = t2.id\n" +
                        "\tLEFT JOIN h_org_department t3 ON t1.belongSq = t3.id\n" +
                        "\tLEFT JOIN h_org_department t4 ON t1.belongWg = t4.id\n" +
                        "where t1.id = '%s'"
                ,id);
        List<Map<String,Object>> list = jpaNativeQuery.getList(sql, null, null);
        return Result.ok(list.get(0));
    }


    @GetMapping("/getList")
    public Result getList(@RequestParam("pageIndex") int pageIndex, @RequestParam("pageSize") int pageSize){
        String sql =String.format("SELECT\n" +
                "\tt1.id,\n" +
                "\tt2.NAME jd,\n" +
                "\tt3.NAME sq,\n" +
                "\tt4.NAME wg,\n" +
                "\tt1.jqbh,\n" +
                "\tt1.bjlx,\n" +
                "\tt1.jjlb,\n" +
                "\tt1.jjxz,\n" +
                "\tt1.bjsj,\n" +
                "\tt1.afdd,\n" +
                "\tt1.bjnr,\n" +
                "\tt1.sszhzxmc,\n" +
                "\tt1.gxdw,\n" +
                "\tt1.bjrxm,\n" +
                "\tt1.bjdh,\n" +
                "\tt1.dhdz,\n" +
                "\tt1.jqmc \n" +
                "FROM\n" +
                "\t`iknn4_sxqdb` t1\n" +
                "\tLEFT JOIN h_org_department t2 ON t1.belongJd = t2.id\n" +
                "\tLEFT JOIN h_org_department t3 ON t1.belongSq = t3.id\n" +
                "\tLEFT JOIN h_org_department t4 ON t1.belongWg = t4.id\n" +
                "limit %s, %s "
                ,pageIndex,pageSize);
        List<Map<String,Object>> list = jpaNativeQuery.getList(sql, null, null);
        return Result.ok(list);
    }




    @PostMapping("/add")
    public void createdSXQD(@RequestBody List<SXQDDto> dtoList){
        // 查询流程发起人
        UserModel originator = organizationFacade.getUserByUsername(ORIGINATOR_USERNAME);
        String userId = originator.getId();
        for (SXQDDto dto : dtoList){
            BizObjectModel bizObjectModel = makeUpData(dto);
            bizObjectModel.setSequenceStatus(WorkflowInstanceStatus.COMPLETED.toString());
            bizObjectFacade.saveBizObjectModel(userId,bizObjectModel,null);
        }
    }

    private BizObjectModel makeUpData(SXQDDto dto){
        Map<String, Object> data = new HashMap<>();
        processjdsqwg(data,dto);
        data.put("jqbh",dto.getPoliceNumber());
        data.put("bjlx",dto.getAlarmType());
        data.put("jjlb",dto.getAlarmCategory());
        data.put("jjxz",dto.getAlarmNature());
        data.put("bjsj",dto.getAlarmTime());
        data.put("afdd",dto.getIncidentPlace());
        data.put("bjnr",dto.getAlarmContent());
        data.put("sszhzxmc",dto.getCenterName());
        data.put("gxdw",dto.getJurisdiction());
        data.put("bjrxm",dto.getAlarmPersonName());
        data.put("bjdh",dto.getAlarmPersonPhone());
        data.put("dhdz",dto.getAlarmPersonAddress());
        data.put("jqmc",dto.getPoliceAreaName());
        return new BizObjectModel(SCHMEA_CODE, data, Boolean.FALSE);
    }

    private void processjdsqwg(Map<String, Object> data,SXQDDto dto){
        String street = dto.getStreet();
        String community = dto.getCommunity();
        String grid = dto.getGrid();
        if (StringUtils.isNotBlank(street)){
            String sql =String.format("select id from h_org_department where name = '%s' limit 1",street);
            List<String> list = jpaNativeQuery.getList(sql, null, null);
            if (CollectionUtils.isNotEmpty(list)){
                data.put("belongJd",list.get(0));
            }
        }
        if (StringUtils.isNotBlank(community)){
            String sql =String.format("select id from h_org_department where name = '%s' limit 1",community);
            List<String> list = jpaNativeQuery.getList(sql, null, null);
            if (CollectionUtils.isNotEmpty(list)){
                data.put("belongSq",list.get(0));
            }
        }
        if (StringUtils.isNotBlank(grid)){
            String sql =String.format("select id from h_org_department where name = '%s' limit 1",grid);
            List<String> list = jpaNativeQuery.getList(sql, null, null);
            if (CollectionUtils.isNotEmpty(list)){
                data.put("belongWg",list.get(0));
            }
        }
    }

}
